package com.test;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JList;
import javax.swing.JLabel;
import javax.swing.JTree;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class mm extends JFrame {

	private JPanel contentPane;
	private JTable jtable;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					mm frame = new mm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public mm() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("New label");
		lblNewLabel.setBounds(184, 11, 46, 14);
		contentPane.add(lblNewLabel);
		
		jtable = new JTable();
		jtable.setModel(new DefaultTableModel(
			new Object[][] {
				{null},
			},
			new String[] {
				"New column"
			}
		));
		jtable.setColumnSelectionAllowed(true);
		jtable.setCellSelectionEnabled(true);
		
		jtable.setBounds(20, 46, 231, 132);
		contentPane.add(jtable);
		
		JButton btnNewButton = new JButton("New button");
		btnNewButton.addActionListener(new ActionListener() {
			//testowy button
			public void actionPerformed(ActionEvent arg0) {
				
				DefaultTableModel model=(DefaultTableModel) jtable.getModel();
				 model.setRowCount(0);
				String nazwa ="elo";
				String nazwa2 = "ms";
				String all = "m1;m2;m3;m4";
				
					
				
					
					
					String result = null;
					 String str = "/usr/local/apache2/resumes/dir1/dir2/dir3/dir4";
					 String regex ="(dir)+[\\d]";
					 Matcher matcher = Pattern.compile( regex ).matcher( str);
					  while (matcher.find( ))
					  {
					  result = matcher.group();     
					  System.out.println(result);                 
					  }
					
					  
					  String[] tokens = all.split(";");
						
						for (String t : tokens)
						{
						 // String m[t] = t;
						System.out.println(t);
						
				 model.addRow(new Object []{
							
							t
						 
		                });
						}
				/* DefaultTableModel model=(DefaultTableModel) jtable.getModel();
			        //czyszczenie
			        model.setRowCount(0);
			        
			      //  model.fireTableDataChanged();
			        String sql="SELECT * FROM kursy AS k JOIN notowania AS n USING ( Lp ) GROUP BY (n.Nazwa)";

			                 
			        try {
			            
			            Class.forName("com.mysql.jdbc.Driver");
			            Connection con= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/gielda","root","");
			            As we are creating a connection on a local computer we will write the url as jdbc:mysql://localhost:3306 
			            Statement stmt=con.createStatement();
			            ResultSet rs = stmt.executeQuery(sql);

			            while (rs.next())
			            {
			               
			                String nazwa= rs.getString("Nazwa");
			                String kurs= rs.getString("Kurs");
			                String obroty= rs.getString("Obroty");
			                		System.out.println(nazwa);
			                model.addRow(new Object []{
			                    nazwa
			                    //kurs,
			                    //obroty
			                });
			    
			            }
			        }
			        catch(Exception e) {
			            //JOptionPane.showMessageDialog(this, e.getMessage());
			        }*/
			}
		});
		btnNewButton.setBounds(73, 207, 89, 23);
		contentPane.add(btnNewButton);
	}
}
